function time() {
  return Number.parseInt(Date.now() / 1000);
}

// function datetime() {
//   return think.datetime(Date.now());
// }

function randString(length, specialChars = false) {
  let chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  if (specialChars) {
    chars += '!@#$%^&*()';
  }

  let result = '';
  const max = chars.length - 1;
  for (let i = 0; i < length; i++) {
    result += chars[rand(0, max)];
  }
  return result;
}

function hash(string, salt = null) {
  /** 生成随机字符串 */
  salt = think.isEmpty(salt) ? randString(9) : salt;
  const length = string.length;
  let hash = '';
  let last = ord(string[length - 1]);
  let pos = 0;

  /** 判断扰码长度 */
  if (salt.length != 9) {
    /** 如果不是9直接返回 */
    return;
  }

  while (pos < length) {
    const asc = ord(string[pos]);
    const saltStr = salt[(last % asc) % 9];
    last = (last * saltStr.charCodeAt() + asc) % 95 + 32;
    hash += String.fromCharCode(last);
    pos++;
  }

  return '$T$' + salt + think.md5(hash);
}

/**
 * 判断hash值是否相等
 * @param {*} from
 * @param {*} to
 */
function hashValidate(from, to) {
  if (to.substring(0, 3) === '$T$') {
    salt = to.substring(3, 9);
    return hash(from, salt) === to;
  } else {
    return think.md5(from) === to;
  }
}
